//
//  ACAlarmEditController.m
//  AlarmClock
//
//  Created by kai ren on 03/09/2011.
//  Copyright 2011 none. All rights reserved.
//

#import "ACAlarmEditController.h"
#import "ACRepeatCheck.h"

@implementation ACAlarmEditController

@synthesize _tableView;

#pragma mark -
#pragma mark Private

#pragma mark -
#pragma mark Unclassified

- (id)init{
	if (self = [super init]) {
		self.title = NSLocalizedString(@"Add Alarm", nil);
		alarm = [[ACAlarm alloc] init];
	}
	
	return self;
}

- (id)initWithAlarm:(ACAlarm *)_alarm{
	if (self = [super init]) {
		self.title = NSLocalizedString(@"Edit Alarm", nil);
		alarm = [_alarm retain];
	}
	
	return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor = [UIColor whiteColor];
	
	//title	
	self.navigationController.navigationBar.tintColor = barColor;
	UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
																			   target:self 
																			   action:@selector(cancelAction)];
	UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
																			   target:self 
																			   action:@selector(saveAction)];
	
	self.navigationItem.rightBarButtonItem = btnSave;
	self.navigationItem.leftBarButtonItem = btnCancel;
	
	[btnCancel release];
	[btnSave release];
	
	//table
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 208) style:UITableViewStyleGrouped];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	
	_tableView.layer.shadowColor = [UIColor blackColor].CGColor;
	_tableView.layer.shadowOpacity = 0.7;
	_tableView.layer.shadowOffset = CGSizeMake(0, 1);
	_tableView.layer.shadowRadius = 4;
	_tableView.backgroundColor = [UIColor clearColor];
	_tableView.scrollEnabled = NO;
	//what's www
	[self.view addSubview:_tableView];
	
	//data picker
	datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 320, 218)];
	datePicker.datePickerMode = UIDatePickerModeTime;
	datePicker.date = alarm.fireTime;
	
	[self.view	addSubview:datePicker];
	
	//background
	self.view.backgroundColor = Background;
}

- (void) viewDidAppear:(BOOL)animated{
	[super viewDidAppear:animated];
	[self._tableView reloadData];
}

- (void)dealloc {
    [super dealloc];
	
	[alarm release];
	[datePicker release];
	[self._tableView release];
}

#pragma mark -
#pragma mark Button Action

- (void)cancelAction{
	[self dismissModalViewControllerAnimated:YES];
}

- (void)saveAction{
	alarm.fireTime = datePicker.date;
	alarm.isActive = YES;
	[alarm writeInfo];
	[alarm removeNotication];
	[alarm addNotification];
	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

	if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
	
	switch (indexPath.row) {
		case 0:
			cell.textLabel.text = NSLocalizedString(@"Repeat", nil);
			
			cell.detailTextLabel.text = stringOfRepeat(alarm);
			break;
		default:
			break;
	}
	
	cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
	cell.textLabel.textAlignment = UITextAlignmentLeft;
	cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
	cell.detailTextLabel.textAlignment = UITextAlignmentRight;
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	
	ACRepeatCheck *checkCtrl = [[ACRepeatCheck alloc] initWithAlarm:alarm];
	[self.navigationController pushViewController:checkCtrl animated:YES];
	[checkCtrl release];
	
	cell.selected = NO;
}

@end
